package cn.team11.sdsPackager;


import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;

import com.aliyun.alink.business.login.AlinkLoginBusiness;
import com.aliyun.alink.sdk.jsbridge.BoneCallback;
import com.aliyun.alink.sdk.jsbridge.IJSBridge;
import com.aliyun.alink.sdk.jsbridge.methodexport.BaseBonePlugin;
import com.aliyun.alink.sdk.jsbridge.methodexport.MethodExported;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cn.team11.smarteye.activity.CateyeDeviceListActivity;
import cn.team11.smarteye.activity.InComingActiviity;
import cn.team11.smarteye.bean.CateyeEvent;
import cn.team11.smarteye.misc.SConstants;
import cn.team11.smarteye.service.CatEyeService;
import cn.team11.smarteye.utils.PreferencesUtils;
import cn.team11.util.T11Log;

/**
 * Created by jtr on 2017/11/24.
 * 猫眼的插件
 */

public class T11EyesPlugin extends BaseBonePlugin{
    public static final String API_NAME = "T11EyesPlugin";
    private ServiceConnection mServiceConn;
    protected CatEyeService mCateyeService;

    @MethodExported
    public void goToEyes(String arg1, BoneCallback boneCallback){
        T11Log.e("跳转猫眼code, " + arg1);
        Intent intent = new Intent(context, CateyeDeviceListActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @MethodExported
    public void logoutYK(String arg1,BoneCallback boneCallback){
        T11Log.e("登出移康code, " + arg1);
        if (mCateyeService != null){
            mCateyeService.cateyeLogout();
        }
    }

    @MethodExported
    public void loginYK(String arg1,BoneCallback boneCallback){
        T11Log.e("登录怡康code, " + arg1);
        PreferencesUtils.putString(SConstants.USERNAME, getUserID());
    }

    @Override
    public void onInitialize(Context context, IJSBridge ijsBridge) {
        super.onInitialize(context, ijsBridge);
        mServiceConn = new CateyeServiceConnection(context);
        context.bindService(new Intent(context, CatEyeService.class), mServiceConn, Context.BIND_AUTO_CREATE);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        context.unbindService(mServiceConn);
    }

    public String getUserID(){
        try {
            if(AlinkLoginBusiness.getInstance().isLogin()){
                String userID = AlinkLoginBusiness.getInstance().getUserID();
                return userID;
            }
        }catch (Exception e){
            T11Log.e(e.toString());
            return "";
        }
        return "";
    }
    class CateyeServiceConnection implements ServiceConnection {

        private Context context;

        public CateyeServiceConnection(Context context) {
            this.context = context;
        }

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            mCateyeService = ((CatEyeService.CateyeServiceBinder)iBinder).getService();
            if (mCateyeService != null){
                mCateyeService.cateyeLogin();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mCateyeService = null;
        }
    }
}
